﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using System.IO;

namespace MichMan.Utilities.UnitTest
{
    [DataContract]
    public class SomeData
    {
        [DataMember]
        public string AString { get; set; }
        [DataMember]
        public int AnInt { get; set; }
    }

    [TestClass]
    public class DataContractTest
    {
        [TestMethod]
        public void TestDataContractUtilities()
        {
            SomeData data = new SomeData() { AString = "AString", AnInt = 100 };

            Assert.IsTrue(DataContractUtility.IsDataContractType(data.GetType()));
            string s = DataContractUtility.DataContractToString(data);
            Assert.IsNotNull(s);
            Assert.IsFalse(String.IsNullOrWhiteSpace(s));
            Assert.IsNotNull(XElement.Parse(s));
            Assert.IsNotNull(DataContractUtility.DataContractToXElement(data));

            XElement xelt = XElement.Parse(s);
            SomeData reconstituted = DataContractUtility.XElementToDataContract<SomeData>(xelt);
            Assert.IsNotNull(reconstituted);
            Assert.AreEqual(data.AString, reconstituted.AString);
            Assert.AreEqual(data.AnInt, reconstituted.AnInt);

            using (MemoryStream stream = new MemoryStream())
            {
                DataContractUtility.WriteDataContract(stream, data);
                Assert.AreNotEqual(0, stream.Length);
                stream.Position = 0;
                SomeData fromFile = DataContractUtility.ReadDataContract<SomeData>(stream);
                Assert.IsNotNull(fromFile);
                Assert.AreEqual(data.AString, fromFile.AString);
                Assert.AreEqual(data.AnInt, fromFile.AnInt);
            }
        }
    }
}
